package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.mapper.LoginMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.ILoginService;
import cn.itsource.ymcc.utils.AssertUtil;
import cn.itsource.ymcc.utils.HttpUtil;
import cn.itsource.ymcc.utils.jwt.JwtUtils;
import cn.itsource.ymcc.utils.jwt.PayloadData;
import cn.itsource.ymcc.vo.AccountLoginVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import sun.net.www.http.HttpClient;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author zongcj
 * @since 2023-09-11
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {
    @Autowired
    private LoginMapper loginMapper;

    private int endTime = 60 * 60 * 24;

//        QueryWrapper<Login> loginQuery = new QueryWrapper<>();
//        loginQuery.eq("username",loginVO.getUsername());
//        loginQuery.eq("type",loginVO.getType());
//第一优化
//        LambdaQueryWrapper<Login> loginLambdaQueryWrapper = new LambdaQueryWrapper<Login>();
//        loginLambdaQueryWrapper.eq(Login::getUsername,loginVO.getUsername());
//        loginLambdaQueryWrapper.eq(Login::getType,loginVO.getType());
    @Override
    public JsonResult accountLogin(AccountLoginVO loginVO) {
        return newLogin(loginVO);
    }

    private JsonResult newLogin(AccountLoginVO loginVO){
        //我能拿到用户名和密码
        String url = "http://localhost:10040/oauth/token?client_id=admin&client_secret=123&grant_type=password&username=%s&redirect_uri=http://www.baidu.com&password=%s";
        String formatUrl = String.format(url, loginVO.getUsername(), loginVO.getPassword());
        //使用密码模式去登录
        //httpUtils 工具类去发送请求
        String s = HttpUtil.sendPost(formatUrl, null);
        //就能得到access_token
        //返回数据就行
        return JsonResult.success().setData(s);
    }

    private JsonResult oldLogin(AccountLoginVO loginVO) {
        //查询用户
        //条件构造器
        LambdaQueryWrapper<Login> queryWrapper = new LambdaQueryWrapper<Login>()
                .eq(Login::getUsername, loginVO.getUsername())
                .eq(Login::getType, loginVO.getType());
        //第二次优化
        Login login = loginMapper.selectOne(queryWrapper);
        //判断用户是否存在
        AssertUtil.isNotNull(login,"用户名或密码不正确!");
        //比较密码  使用springSecurity 密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(loginVO.getPassword(), login.getPassword());
        AssertUtil.isTrue(matches,"用户名或密码不正确!");
        String token = createToken(loginVO, login);

        Map<String,Object> result = new HashMap<>();
        result.put("token",token);
        login.setPassword("");
        result.put("user",login);
        return JsonResult.success().setData(result);
    }

    //生成token
    private String createToken(AccountLoginVO loginVO, Login login) {
        //登录成功 生产token
//        String s = UUID.randomUUID().toString();
        //JWT的token
        PayloadData payloadData = new PayloadData();
        payloadData.setLogin(login);
        //todo 后面来补充我们的登录信息
        if(RConst.Num.INT_0 == loginVO.getType()){
//            payloadData.setAdmin();
              //只有管理员需要 权限 菜单
        }else {
//            payloadData.setUser();
        }
        return JwtUtils.createToken(payloadData, endTime);
    }

//    public static void main(String[] args) {
//        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
//        String encode = bCryptPasswordEncoder.encode("123456");
//        System.out.println(encode);
//
//
//        boolean matches = bCryptPasswordEncoder.matches("123456", encode);
//        System.out.println(matches);
//    }
}
